package devent.phase1;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;

import android.content.Intent;
import android.location.LocationManager;
import android.os.SystemClock;
import android.provider.Settings;

public class GPSCheck extends Plugin
{
/**
 * Executes the request and returns PluginResult.
 *
 * @param action        The action to execute.
 * @param args          JSONArry of arguments for the plugin.
 * @param callbackId    The callback id used when calling back into JavaScript.
 * @return              A PluginResult object with a status and message.
 */
	
public PluginResult execute(String action, JSONArray args, String callbackId)
{
	boolean gpsEnabled = false;
	
	android.content.ContentResolver contentResolver = cordova.getActivity().getApplicationContext().getContentResolver();
    gpsEnabled = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
    
    if(gpsEnabled==false)
    {
    	//show the location settings android default page
    	this.cordova.getActivity().startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
    	SystemClock.sleep(3000);//deliberate time delay to allow our next check to work correctly
    	//we dont know if the user has enabled gps at this point
    	gpsEnabled = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
    	if(gpsEnabled==false)//the user has still not enabled the gps
    		return new PluginResult(PluginResult.Status.ERROR,"false");
    	return new PluginResult(PluginResult.Status.OK,"true");//yaaay !!
    }
    else
    	return new PluginResult(PluginResult.Status.OK,"true");//gps is already on
}

}
